package triTree;

import transaction.Transaction;
import triStorage.*;
import utils.Constants;

public class RemovableTriSet {
	
	private IReader reader;
	private IRandIntAccess access;
	private long pos;
	
	public RemovableTriSet(IReader reader, IRandIntAccess access) {
		this.reader = reader;
		this.access = access;
		this.pos = -1;
	}
	
	private String readNextItem() {
		Integer flag;
		while (true) {
			flag = reader.readInt();
			if (flag == null)
				return null;
			pos = reader.getReadPos();
			if (flag < 0)
				return null;
			else if (flag == 0)
				reader.skipString();
			else
				break;
		}
		
		return reader.readString();
	}
	
	//time-consuming
	public void removeItem(String item, Transaction tx) {
		String str;
		while (true) {
			str = readNextItem();
			if (str == null)
				return;
			if (str.equals(item)) {
				access.writeInt(pos - Constants.INT_SIZE, 0, tx);
				return;
			}
		}
	}

	public void dispose() {
		access.dispose();
	}
}
